package com.ktjy.ch15.service.impl;

import com.ktjy.ch15.entity.Flower;
import com.ktjy.ch15.mapper.FlowerMapper;
import com.ktjy.ch15.service.FlowerService;
import com.ktjy.ch15.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("flowerService")
public class FlowerServiceImpl implements FlowerService {

    @Autowired
    FlowerMapper flowerMapper;

    @Override
    public Page<Flower> findPage(Flower flower, Integer pageNo, Integer pageSize) {
        Page<Flower> page = new Page<Flower>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 查询总数，计算总页数
        Integer count = flowerMapper.count(flower);
        page.setTotalCount(count);
        // 查询分页数据
        Integer pageBegin=(pageNo-1)*pageSize;
        List<Flower> data = flowerMapper.getPage(flower, pageBegin, pageSize);
        page.setData(data);
        return page;
    }

    @Override
    public boolean save(Flower flower) {
        return flowerMapper.insert(flower) > 0;
    }

    @Override
    public Flower findById(Integer flowerId) {
        return flowerMapper.getById(flowerId);
    }

    @Override
    public boolean update(Flower flower) {
        return flowerMapper.update(flower) > 0;
    }

    @Override
    public boolean delete(Integer flowerId) {
        return flowerMapper.delete(flowerId)> 0;
    }

    @Override
    public Flower findByFlowerName(String flowerName) {
        return flowerMapper.getByFlowerName(flowerName);
    }

}
